<?php
namespace app\api\controller\v1\member;
use app\api\controller\v1\Init;

class Fans extends Init
{
	public function _initialize() {
		parent::_initialize();
		$this->model = model('member/fans', 'service');
	}

	/* 成员列表 */
	public function lists(){
		$info = $this->model->lists([], $this->params['limit'], $this->params['page']);
		foreach ($info as $key => $value) {
			//关联模型查询
			if($this->params['ismember'] === true) $value->member;
			if($this->params['islive'] === true) $value->live;
		}
		return $this->response(lang('Operation completed'), $info);
	}

	/* 成员详情 */
	public function info() {
		$maps = [];
		$maps['openid'] = $this->params['openid'];
		$info = $this->model->get($maps);
		//关联模型查询
		if($this->params['ismember'] === true) $info->member;
		if($this->params['islive'] === true) $info->live;
		return $this->response(lang('Operation completed'), $info);
	}

	/* 成员添加 */
	public function add() {
		$post = $this->request->post();
		$result = $this->model->add($post, [], 'add');
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}

	/* 成员编辑 */
	public function edit() {
		$post = $this->request->post();
		$result = $this->model->edit($post, ['openid' => $post['openid']], 'edit');
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}

	/* 成员删除 */
	public function delete() {
		$id = $this->request->post('openid/s');
		$ids = explode(",", $id);
		$maps = ['openid' => ['in', $ids]];
		$result = $this->model->where($maps)->delete();
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}
}